#!/usr/bin/env/ pythonBasics
# -*-coding:utf-8 -*-

# Author:super
import pytest
from base.apiRequest import ApiRequest
import json

obj = ApiRequest()

def selectAll(token):
	'''
	查询所有书籍
	:param token: 登录后的token值
	:return:
	'''
	headers = {'Authorization': 'jwt {0}'.format(token)}
	req = obj.get(url='http://127.0.0.1:5000/v1/api/books',
	                   headers=headers)
	print(json.dumps(req.json(), indent=True, ensure_ascii=False))
	assert 'PythonApiauth' in json.dumps(req.json(), indent=True, ensure_ascii=False)


def add(token,datas):
	'''
	添加书籍
	:param token: 登录的token
	:param datas: 数据
	:return: 返回添加的书籍id
	'''
	headers = {'Authorization': 'jwt {0}'.format(token)}
	req = obj.post(url='http://127.0.0.1:5000/v1/api/books',
	                    headers=headers,
	                    json=datas['data'])
	assert '添加书籍成功' in json.dumps(req.json(),ensure_ascii=False)
	print(json.dumps(req.json(), indent=True, ensure_ascii=False))
	return req.json()[0]['datas']['id']

def update(token,datas,id):
	'''
	修改书籍信息
	:param token: 登录的token
	:param datas: 数据
	:param id: 书籍id
	:return:
	'''
	headers = {'Authorization': 'jwt {0}'.format(token)}
	req = obj.put(url='http://127.0.0.1:5000/v1/api/book/{0}'.format(id),
	              headers=headers,
	              json=datas['data'])
	assert '更新书的信息成功' in json.dumps(req.json(),ensure_ascii=False)
	print(json.dumps(req.json(), indent=True, ensure_ascii=False))

def delete(token,id):
	'''
	删除书籍信息
	:param token: 登录后的token
	:param id: 要删除的书籍id
	:return:
	'''
	headers = {'Authorization': 'jwt {0}'.format(token)}
	req = obj.delete(url='http://127.0.0.1:5000/v1/api/book/{0}'.format(id),
	              headers=headers)
	assert '删除书籍成功' in json.dumps(req.json(), ensure_ascii=False)
	print(json.dumps(req.json(), indent=True, ensure_ascii=False))

def selectID(token,id):
	'''
	查询书籍信息
	:param token: 登录后的token
	:param id: 要查询的书籍id
	:return:
	'''
	headers = {'Authorization': 'jwt {0}'.format(token)}
	req = obj.get(url='http://127.0.0.1:5000/v1/api/book/{0}'.format(id),
	              headers=headers)
	assert 'API测试实战' in json.dumps(req.json(), ensure_ascii=False)
	print(json.dumps(req.json(), indent=True, ensure_ascii=False))